package com.goldsprite.microhorigame.screens.examples.simplephysexamples;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.goldsprite.gameframeworks.timeline.TimeLineTask;
import com.goldsprite.gameframeworks.assets.GameRes;
import com.goldsprite.gameframeworks.ecs.component.RigidbodyComponent;
import com.goldsprite.gameframeworks.ecs.entity.GObject;
import com.goldsprite.gameframeworks.ecs.system.GameSystem;
import com.goldsprite.gameframeworks.log.Debug;
import com.goldsprite.gameframeworks.screens.GScreen;
import com.goldsprite.gameframeworks.utils.math.Vector2;
import com.goldsprite.microhorigame.screens.games.CameraTool;
import com.goldsprite.microhorigame.systems.GObjectGenerator;

public class EcsPhysSimpleDemo extends GScreen {
	private GameSystem gm;
	private OrthographicCamera worldCamera;
	private Vector2 worldCamPos = new Vector2();
	private float[] nextCamZoom = {1};
	private GObject rat;
	private RigidbodyComponent ratRigi;

	@Override
	public void create() {
		gm = new GameSystem(getViewport());
		worldCamera = GameSystem.worldCamera;

		//相机缩放操作
		getImp().addProcessor(CameraTool.createCamZoomEvent(worldCamera, new Stage(getViewport()), nextCamZoom));
		getImp().addProcessor(CameraTool.createCamController(worldCamera));

		createGObjects();

//		TimeLineTask.newThread(
//			TimeLineTask.sequence(
//				(e) -> {
//					Debug.log("老鼠在天往下掉");
//					ratRigi.getPosition().add(0, 200);
//					ratRigi.getVelocity().add(0, -8);
//				},
//				(e) -> Debug.log("老鼠开始落地"),
//				TimeLineTask.threadWait(() -> {
//					boolean condition = ratRigi.getVelocity().y == 0;
//					return condition;
//				}),
//				(e) -> Debug.log("老鼠落地"),
//				(e) -> {
//					Debug.log("老鼠起跳");
//					ratRigi.setVelocity(0, 500);
//				},
//				(e) -> gm.pauseGameSystem()
//			)
//		);

		{
			TimeLineTask customTimeLineTask = TimeLineTask.sequence(
				e -> {
					Debug.log("老鼠在天往下掉");
					ratRigi.addPosition(0, 200);
					ratRigi.addVelocity(0, -20);
				},

				e -> Debug.log("老鼠开始落地"),

				e -> e.isCompleted = ratRigi.getVelocity().y == 0,

				e -> Debug.log("老鼠落地"),

				e -> {
					Debug.log("老鼠起跳");
					ratRigi.setVelocity(0, 500);
				},

				e -> gm.pauseGameSystem()
			);
			GameSystem.timeLineTaskSystem.executeTimeLineTask(customTimeLineTask);
		}
	}

	private void createGObjects() {
		GObjectGenerator.newWall(GameRes.Blue, 100, 100, 50, 50);

		rat = GObjectGenerator.newDemonRat(100, 150);
		ratRigi = rat.getComponent(RigidbodyComponent.class);
	}

	Vector2 tmpVec2 = new Vector2();

	@Override
	public void render0(float delta) {
		drawWorldBack();

		gm.gameLoop(delta);

		//相机缩放
		float zoom = worldCamera.zoom;
		float interpolation = delta * 3f;
//		interpolation = 1;//取消插值
		zoom = MathUtils.lerp(zoom, nextCamZoom[0], interpolation);//插值
		zoom = Math.max(0.03f, zoom);//限制
		worldCamera.zoom = zoom;//更新
	}

	SpriteBatch batch = new SpriteBatch();

	private void drawWorldBack() {
		batch.setProjectionMatrix(worldCamera.combined);
		batch.begin();
		batch.draw(GameRes.Orange.getResource(Texture.class), 0, 0, getViewSize().x, getViewSize().y);
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
		gm.resize(width, height);
	}
}
